package com.hunan.api.catv.service.biz.member.white;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.dto.MemberWhiteDto;
import com.hunan.api.catv.entity.po.MemberWhitelist;
import com.hunan.api.catv.entity.vo.MemberVo;
import com.hunan.api.catv.entity.vo.MemberWhitelistVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.member.MemberBiz;
import com.hunan.api.catv.service.impl.member.white.MemberWhiteService;
import com.hunan.api.catv.utils.EasyExportUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: wangchao
 * @Date: 2020/11/12 17:51
 */
@Service
public class MemberWhiteBiz extends ResultService {

    @Autowired
    MemberBiz memberBiz;

    @Autowired
    MemberWhiteService memberWhiteService;

    /**
     * 导入用户白名单
     */
    public Object importMemberWhitelist(MultipartFile file, Integer sysUserId) throws IOException {
        try {
            //String filePath = "C:\\Users\\wangchao\\Desktop\\白名单.xlsx";
            List<MemberWhiteDto> memberWhiteDtoList = EasyExportUtils.importExcel(file, 0, 1, MemberWhiteDto.class);
            log.info("导入数据一共【" + memberWhiteDtoList.size() + "】行");
            ArrayList<MemberWhitelist> list = new ArrayList<>();
            for (MemberWhiteDto memberWhiteDto : memberWhiteDtoList) {
                //通过手机号查询用户信息
                MemberVo memberVo = memberBiz.getMemberInfoByPhone(memberWhiteDto.getPhone());
                if (memberVo == null) {
                    log.error("此用户查询不到,手机号:{}", memberWhiteDto.getPhone());
                    continue;
                }
                MemberWhitelist memberWhitelist1 = new MemberWhitelist().selectOne(new QueryWrapper<MemberWhitelist>().eq(MemberWhitelist.MEMBER_ID, memberVo.getMemberId()));
                if (memberWhitelist1 != null) {
                    continue;
                }
                MemberWhitelist memberWhitelist = new MemberWhitelist();
                memberWhitelist.setMemberId(memberVo.getMemberId())
                        .setMemberType(memberWhiteDto.getType())
                        .setCreateTime(new Date())
                        .setUpdateTime(new Date())
                        .setStatus(1)
                        .setCreateBy(sysUserId)
                        .setDeleted(1);
                list.add(memberWhitelist);
            }
            if (CollectionUtils.isEmpty(list)) {
                return null;
            }
            //批量插入
            ServiceResult serviceResult = memberWhiteService.batchAdd(list);
            log.info("批量插入,list:{},插入成功:{}", list.size(), serviceResult.data);
        } catch (Exception e) {
            log.error(e.toString());
        }
        return null;
    }

    /**
     * 白名单列表
     *
     * @return
     */
    public PageInfo<MemberWhitelistVo> getMemberWhitelist(int pageNum, int pageSize, String nickName, String phone, Integer status) {
        MemberWhitelistVo memberWhitelistVo = new MemberWhitelistVo();
        memberWhitelistVo.setPhone(phone);
        memberWhitelistVo.setStatus(status);
        memberWhitelistVo.setNickName(nickName);
        PageHelper.startPage(pageNum, pageSize);
        List<MemberWhitelistVo> memberWhiteList = memberWhiteService.getMemberWhiteList(memberWhitelistVo);
        return new PageInfo<>(memberWhiteList);
    }


    /**
     * 获取用户白名单详情
     */
    public MemberWhitelistVo getMemberWhitelistInfo(Integer memberWhitelistId) {
        MemberWhitelist memberWhitelist = new MemberWhitelist().selectById(memberWhitelistId);
        MemberWhitelistVo memberWhitelistVo = new MemberWhitelistVo();
        BeanUtils.copyProperties(memberWhitelist, memberWhitelistVo);
        memberWhitelistVo.setCreateName(memberWhiteService.getSystemUserName(memberWhitelist.getCreateBy()));
        memberWhitelistVo.setUpdateName(memberWhiteService.getSystemUserName(memberWhitelist.getUpdateBy()));
        return memberWhitelistVo;
    }


    /**
     * 批量修改用户状态
     * type 0 -- 禁用
     * 1 -- 启用
     */
    public ServiceResult updateMemberWhitelist(String memberWhitelistIds, String type, Integer sysUserId) {
        List<String> stringList = Stream.of(memberWhitelistIds.split(",")).collect(Collectors.toList());
        for (String id : stringList) {
            MemberWhitelist memberWhitelist = new MemberWhitelist().selectById(id);
            if (memberWhitelist == null) {
                return buildFail(ConstantsMsg.admin_edit_fail);
            }
            if ("0".equals(type)) {
                //批量禁用
                memberWhitelist.setStatus(0);
            } else {
                //批量启用
                memberWhitelist.setStatus(1);
            }
            boolean update = memberWhitelist
                    .setUpdateTime(new Date())
                    .setUpdateBy(sysUserId)
                    .update(new QueryWrapper<MemberWhitelist>().eq(MemberWhitelist.ID, Integer.parseInt(id)));
            if (!update) {
                return buildFail(false, ConstantsMsg.admin_edit_fail);
            }
        }
        return buildOK(true, ConstantsMsg.admin_edit_success);
    }
}
